package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.entity.YcSysUserDepart;
import com.yuncheng.entity.YcSysUserRole;
import com.yuncheng.oaApi.bxdService.OaSpcyJcyFybxdService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.constant.SysUserConstant;
import com.yuncheng.spcyApi.dto.SysJobDto;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.IYcSysUserService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.vo.GetKsKz;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class YcSysUserServiceImpl extends BaseServiceImpl<YcSysUserMapper, YcSysUser> implements IYcSysUserService {

    private static final Logger log = LoggerFactory.getLogger(YcSysUserServiceImpl.class);

    @Resource
    private YcSysUserMapper ycSysUserMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private OaSpcyJcyFybxdService oaSpcyJcyFybxdService;

    @Override
    public YcSysUser getYcSysUserById(String userId) {
        YcSysUser ycSysUser = getById(userId);
        if (ycSysUser != null){
            return ycSysUser;
        }
        return null;
    }

    @Override
    public List<GetKsKz> getKsKzObject(String deptName, String zw) {
        deptName = SpcyConstant.getYwclNameByYWMC(deptName);
        List<GetKsKz> ksKzObject = ycSysUserMapper.getKsKzObject(deptName, zw);
        return ksKzObject;
    }

    @Override
    public List<YcSysUser> getUserListByDeptId(String deptId) {
        return ycSysUserMapper.selectUserListByDeptId(deptId);
    }

    @Override
    public void verifyUserInAccount(String oldAccount, String newAccount) {
        // 账号不同
        if (StringUtils.isNotBlank(newAccount)){
            if (StringUtils.isBlank(oldAccount) || !oldAccount.equals(newAccount)){
                List<YcSysUser> userList = ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                        .eq(YcSysUser::getUsername, newAccount));
                if (CollectionUtils.isNotEmpty(userList)){
                    throw new BusinessException("该账号已经存在了！");
                }
            }
        }
    }

    @Override
    public void verifyUserInPhone(String oldPhone, String newPhone) {
        // 手机号不同
        if (StringUtils.isNotBlank(newPhone)){
            if (StringUtils.isBlank(oldPhone) || !oldPhone.equals(newPhone)){
                List<YcSysUser> phoneList = ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                        .eq(YcSysUser::getPhone, newPhone));
                if (CollectionUtils.isNotEmpty(phoneList)){
                    throw new BusinessException("该手机号已经存在了！");
                }
            }
        }
    }

    @Override
    public void verifyUserInSfzh(String oldFsfzh, String newFSfzh) {
        // 身份证号
        if (StringUtils.isNotBlank(newFSfzh)){
            if (StringUtils.isBlank(oldFsfzh) || !oldFsfzh.equals(newFSfzh)){
                List<YcSysUser> sfzhList = ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                        .eq(YcSysUser::getFsfzh, newFSfzh));
                if (CollectionUtils.isNotEmpty(sfzhList)){
                    throw new BusinessException("该身份证号已经存在了！");
                }
            }
        }
    }

    @Override
    public List<YcSysUser> selectUserByPhone(String phone) {
        return ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getPhone, phone));
    }

    @Override
    public List<YcSysUser> selectUserBySfzh(String sfzh) {
        return ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getFsfzh, sfzh));
    }

    @Override
    public List<YcSysUser> selectUserByAccount(String account) {
        return ycSysUserMapper.selectList(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getUsername, account));
    }

    @Override
    public void updateSysUser(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        String id = "";
        try {
            id = buffer.get("id");
        }catch (Exception e){

        }

        if (StringUtils.isBlank(id)){
            id = GetCurrentUserUtils.getCurrentUserId();
        }

        YcSysUser user = ycSysUserMapper.selectById(id);

        if (user == null){
            throw new BusinessException("没有相匹配的数据!");
        }

        String oldUsername = user.getUsername(); // 账号
        String oldPhone = user.getPhone(); // 手机号
        String oldFsfzh = user.getFsfzh(); // 身份证号

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        Map<String, Object> map = ObjectToMapUtils.objectToMap(user);
        try {
            BeanUtils.populate(user,buffer);
        } catch (IllegalAccessException e) {
            log.error("更新用户信息出错【IllegalAccessException】"+e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("更新用户信息出错【InvocationTargetException】"+e.getMessage());
        }
        verifyUserInAccount(oldUsername, user.getUsername());
        verifyUserInPhone(oldPhone, user.getPhone());
        verifyUserInSfzh(oldFsfzh, user.getFsfzh());

        // 计算工作年限
        if (StringUtils.isNotBlank(user.getFcsjckssj())){
            String jcYear = user.getFcsjckssj().substring(0, 4);
            String jgYear = new BigDecimal(DateConversionUtils.DateToYear(new Date())).subtract(new BigDecimal(jcYear)).toString();
            user.setFgznx(jgYear);
        }

        user.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        user.setUpdateTime(new Date());
        ycSysUserMapper.updateById(user);

        String logNr = "修改个人信息: ";
        logNr += LogUtils.BeanModifyMsg(user,map);

        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,logNr,id,LogConstant.LOGS_SYS_USER,SpcyConstant.LOGS_SYS_TYPE_XG);

        /** 银行卡号不为空，同步报销单个人记录银行卡信息 */
        if (StringUtils.isNotBlank(user.getFyhkh())){
            oaSpcyJcyFybxdService.syncBxdGrjlUserJbxx(id, user);
        }
    }

    @Override
    public YcSysUser genJcyUserAccount(YcSysUser newUser, String userId, String newAccount) {
        newUser.setId(userId);
        newUser.setUsername(newAccount);
        newUser.setPassword(SysUserConstant.PASSWORD); // 默认密码
        newUser.setSalt(SysUserConstant.SALT); // 加盐值
        newUser.setIsRealUser(SysUserConstant.IS_REAL_USER); // 真实用户
        newUser.setWorkNo(userId); // 工号
        newUser.setStatus(SysUserConstant.STATUS_YES); // 有效状态
        newUser.setDelFlag(SysUserConstant.DEL_FLAG_YES); // 删除-有效状态

        newUser.setOrgId(SpcyConstant.HCY_ORGID); // 部门编号
        newUser.setOrgName(SpcyConstant.HCY_ORGNAME); // 部门名称
        newUser.setOrgCode(SpcyConstant.HCY_ORGCODE); // 默认机构编码

        newUser.setFgzzt(SpcyConstant.JCY_ZT_ZG);

        return newUser;
    }

    @Override
    public YcSysUser cancelGenJcyUserAccount(YcSysUser newUser) {
        newUser.setUsername("");
        newUser.setPassword(""); // 默认密码
        newUser.setSalt(""); // 加盐值

        newUser.setIsRealUser(""); // 真实用户
        newUser.setWorkNo(""); // 工号
        newUser.setStatus(0); // 有效状态

        newUser.setOrgId(""); // 部门编号
        newUser.setOrgName(""); // 部门名称
        newUser.setOrgCode(""); // 默认机构编码

        newUser.setFgzzt(SpcyConstant.JCY_ZT_ZG);

        return newUser;
    }

    @Override
    public String getUuidAccount(String nowAccount, String fsfzh) {
        // 验证姓名为账户是否存在
        List<YcSysUser> realnameUser = selectUserByAccount(nowAccount);
        if (CollectionUtil.isNotEmpty(realnameUser) && realnameUser.size() > 0){
            // 姓名+身份证号后四位
            String account = nowAccount + StringPatternUtils.subStringLastFourDigit(fsfzh);
            List<YcSysUser> sfzNameUser = selectUserByAccount(account); // 验证 姓名+身份证后四位 为账户是否存在

            if (CollectionUtil.isNotEmpty(sfzNameUser) && sfzNameUser.size() > 0){
                /*StringBuffer msg = new StringBuffer("添加失败！");
                msg.append("姓名为[").append(nowAccount).append("]，");
                msg.append("账号已经存在！");
                throw new BusinessException(msg.toString());*/
                return GetUuIdUtils.ReplaceUuId();
            }else {
                 return account; // 姓名+身份证后四位=账号
            }
        }else {
            return nowAccount;
        }
    }

    @Override
    public void genJcyUserDeptGx(String userId) {
        // 新增部门用户关系
        YcSysUserDepart ycSysUserDepart = new YcSysUserDepart();
        ycSysUserDepart.setId(GetUuIdUtils.ReplaceUuId());
        ycSysUserDepart.setUserId(userId);
        ycSysUserDepart.setDepId(SpcyConstant.HCY_ORGID); // 核查员部门编号
        ycSysUserDepart.setPositionId(""); // 职务编号
        ycSysUserDepart.setIsMajorDep(SysUserConstant.IS_MAJOR_DEP); // 是否主部门
        ycSysUserMapper.addYcSysUserDept(ycSysUserDepart);
    }

    @Override
    public void genJcyUserRoleGx(String userId) {
        // 新增检查员角色-用户关系
        YcSysUserRole userRole = new YcSysUserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(SpcyCommonConstant.JCY_ROLE_ID);
        List<YcSysUserRole> userRoleList = ycSysUserMapper.getYcUserRole(userRole);
        if (CollectionUtil.isEmpty(userRoleList)) {
            userRole.setId(GetUuIdUtils.ReplaceUuId());
            ycSysUserMapper.addYcSysUserRole(userRole);
        }
    }

    @Override
    public void genJcyUserJobGx(String userId) {
        // 新增检查员岗位-用户关系
        SysJobDto userGw = new SysJobDto();
        userGw.setId(SpcyCommonConstant.JCY_GW_ID);
        List<SysJobDto> userGwList = ycSysUserMapper.getUserGwByUserId(userGw, userId);
        if (CollectionUtil.isEmpty(userGwList)){
            ycSysUserMapper.addSysUserGw(GetUuIdUtils.ReplaceUuId(),userId,SpcyCommonConstant.JCY_GW_ID);
        }
    }

    @Override
    public void deleteJcyUserDeptGx(String userId) {
        ycSysUserMapper.deleteUserDept(userId, SpcyConstant.HCY_ORGID);
    }

    @Override
    public void deleteJcyUserRoleGx(String userId) {
        ycSysUserMapper.deleteUserRole(userId, SpcyCommonConstant.JCY_ROLE_ID);
    }

    @Override
    public void deleteJcyUserJobGx(String userId) {
        ycSysUserMapper.deleteUserGw(userId, SpcyCommonConstant.JCY_GW_ID);
    }

    @Override
    public Page<YcSysUser> getPrSdtjryPageList(YcSysUser query, String gjz, PageVo pageVo) {
        Page<YcSysUser> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<YcSysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getRealname()), YcSysUser::getRealname, query.getRealname());
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getFgzdw()), YcSysUser::getFgzdw, query.getFgzdw());
        queryWrapper.eq(YcSysUser::getStatus, 1);
        queryWrapper.eq(YcSysUser::getDelFlag, 0);
        queryWrapper.isNotNull(YcSysUser::getRealname);
        queryWrapper.ne(YcSysUser::getFgzzt, SpcyConstant.JCY_ZY_JP);

        if (org.apache.commons.lang3.StringUtils.isNotBlank(gjz)){

            int selectSex = 3;
            if (gjz.contains("男")){
                selectSex = 1;
            }else if (gjz.contains("女")){
                selectSex = 2;
            }
            Integer finalSelectSex = selectSex;

            queryWrapper
                    .and(f->f.like(YcSysUser::getRealname, gjz)
                            .or()
                            .like(YcSysUser::getFsfzh, gjz)
                            .or()
                            .like(YcSysUser::getFsxzy, gjz)
                            .or()
                            .like(YcSysUser::getFxl, gjz)
                            .or()
                            .like(YcSysUser::getFgzdw, gjz)
                            .or()
                            .like(YcSysUser::getPhone, gjz)
                            .or()
                            .eq(YcSysUser::getSex, finalSelectSex)
                    );
        }
        queryWrapper.orderByDesc(YcSysUser::getCreateTime);

        return ycSysUserMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<YcSysUser> getJpSdtjryPageList(YcSysUser query, String gjz, PageVo pageVo) {
        Page<YcSysUser> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<YcSysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getRealname()), YcSysUser::getRealname, query.getRealname());
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getFgzdw()), YcSysUser::getFgzdw, query.getFgzdw());

        queryWrapper.ne(YcSysUser::getFgzzt, SpcyConstant.JCY_ZY_JP);
        queryWrapper.eq(YcSysUser::getDelFlag, 0);
        queryWrapper.eq(YcSysUser::getStatus, 1);

        queryWrapper.isNotNull(YcSysUser::getRealname);

        if (org.apache.commons.lang3.StringUtils.isNotBlank(gjz)){

            int selectSex = 3;
            if (gjz.contains("男")){
                selectSex = 1;
            }else if (gjz.contains("女")){
                selectSex = 2;
            }
            Integer finalSelectSex = selectSex;

            queryWrapper
                    .and(f->f.like(YcSysUser::getRealname, gjz)
                            .or()
                            .like(YcSysUser::getFsfzh, gjz)
                            .or()
                            .like(YcSysUser::getFgzdw, gjz)
                            .or()
                            .like(YcSysUser::getFsxzy, gjz)
                            .or()
                            .like(YcSysUser::getFxl, gjz)
                            .or()
                            .like(YcSysUser::getPhone, gjz)
                            .or()
                            .eq(YcSysUser::getSex, finalSelectSex)
                    );
        }
        queryWrapper.orderByDesc(YcSysUser::getCreateTime);

        return ycSysUserMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<YcSysUser> getFybxdUserPageList(YcSysUser query, String gjz, PageVo pageVo) {
        Page<YcSysUser> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<YcSysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getRealname()), YcSysUser::getRealname, query.getRealname());
        queryWrapper.like(org.apache.commons.lang3.StringUtils.isNotBlank(query.getFgzdw()), YcSysUser::getFgzdw, query.getFgzdw());
        queryWrapper.eq(YcSysUser::getStatus, 1);
        queryWrapper.eq(YcSysUser::getDelFlag, 0);
        queryWrapper.isNotNull(YcSysUser::getRealname);

        if (org.apache.commons.lang3.StringUtils.isNotBlank(gjz)){

            int selectSex = 3;
            if (gjz.contains("男")){
                selectSex = 1;
            }else if (gjz.contains("女")){
                selectSex = 2;
            }
            Integer finalSelectSex = selectSex;

            queryWrapper
                    .and(f->f.like(YcSysUser::getRealname, gjz)
                            .or()
                            .like(YcSysUser::getFsfzh, gjz)
                            .or()
                            .like(YcSysUser::getFgzdw, gjz)
                            .or()
                            .like(YcSysUser::getFsxzy, gjz)
                            .or()
                            .like(YcSysUser::getFxl, gjz)
                            .or()
                            .like(YcSysUser::getPhone, gjz)
                            .or()
                            .eq(YcSysUser::getSex, finalSelectSex)
                    );
        }
        queryWrapper.orderByDesc(YcSysUser::getCreateTime);

        return ycSysUserMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void updateUserGzztInZg(String userId) {
        if (StringUtils.isBlank(userId)){
            return;
        }
        LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(YcSysUser::getId, userId);

        YcSysUser updateBean = new YcSysUser();
        updateBean.setFgzzt(SpcyConstant.JCY_ZT_ZG);
        ycSysUserMapper.update(updateBean, updateWrapper);
    }

    @Override
    public void updateUserGzztInZg(List<String> idsList) {
        if (CollectionUtil.isEmpty(idsList)){
            return;
        }

        LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(YcSysUser::getId, idsList);

        YcSysUser updateBean = new YcSysUser();
        updateBean.setFgzzt(SpcyConstant.JCY_ZT_ZG);
        ycSysUserMapper.update(updateBean, updateWrapper);
    }

    @Override
    public void updateUserGzztInJp(String userId) {
        if (StringUtils.isBlank(userId)){
            return;
        }
        LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(YcSysUser::getId, userId);

        YcSysUser updateBean = new YcSysUser();
        updateBean.setFgzzt(SpcyConstant.JCY_ZY_JP);
        ycSysUserMapper.update(updateBean, updateWrapper);
    }

    @Override
    public void updateUserGzztInZxrw(String userId) {
        if (StringUtils.isBlank(userId)){
            return;
        }
        LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(YcSysUser::getId, userId);

        YcSysUser updateBean = new YcSysUser();
        updateBean.setFgzzt(SpcyConstant.JCY_ZY_ZXRWZ);
        ycSysUserMapper.update(updateBean, updateWrapper);
    }

    @Override
    public String getUserZbmByUserId(String userid) {
        if (StringUtils.isBlank(userid)){
            return "0";
        }

        List<String> list = ycSysUserMapper.getUserZbmByUserId(userid);
        if (CollectionUtil.isNotEmpty(list)){
            return list.get(0);
        }

        return "0";
    }

    @Override
    public YcSysUser getUserYhkInfo(String userid) {
        return ycSysUserMapper.getUserYhkInfo(userid);
    }

    @Override
    public YcSysUser getUserJbxxById(String userid) {
        return ycSysUserMapper.findUserJbxxById(userid);
    }

    @Override
    public String verifyUserIsZdgwry(String userid, String jobName) {
        return ycSysUserMapper.verifyUserIsZdgwry(userid, jobName);
    }

    @Override
    public List<YcSysUser> getDeptIndexUserList(String sysorgid) {
        return ycSysUserMapper.getDeptIndexUserList(sysorgid);
    }

    @Override
    public List<YcSysUser> findUserJbxxList(String ids) {
        return ycSysUserMapper.findUserJbxxList(ids);
    }

    @Override
    public List<YcSysUser> findUserJbxxListByIds(List<String> ids) {
        return ycSysUserMapper.findUserJbxxListByIds(ids);
    }

    @Override
    public List<YcSysUser> findRealnameListByIds(List<String> ids) {
        return ycSysUserMapper.findRealnameListByIds(ids);
    }

    @Override
    public String findFgzztById(String id) {
        return ycSysUserMapper.findFgzztById(id);
    }

    @Override
    public boolean verifyUserFgzztIsZg(String userid) {
        String fgzzt = this.findFgzztById(userid);
        if (StringUtils.isBlank(fgzzt)){
            return true;
        }

        if (fgzzt.equals(SpcyConstant.JCY_ZT_ZG)){
            return true;
        }

        return false;
    }

    @Override
    public boolean verifyUserFgzztIsJp(String userid) {
        String fgzzt = this.findFgzztById(userid);
        if (StringUtils.isBlank(fgzzt)){
            return false;
        }

        if (fgzzt.equals(SpcyConstant.JCY_ZY_JP)){
            return true;
        }

        return false;
    }

    @Override
    public String findPhoneById(String id) {
        return ycSysUserMapper.findPhoneById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addQyAccountUser(String userId, String username, String realname, SpcyQyJbxx qy) {

        /** 新增企业用户 */
        if (StringUtils.isBlank(userId)) {
            userId = GetUuIdUtils.ReplaceUuId();
        }

        YcSysUser sysUser = new YcSysUser();
        sysUser.setId(userId);
        sysUser.setPassword(SysUserConstant.PASSWORD);
        sysUser.setSalt(SysUserConstant.SALT);
        sysUser.setWorkNo(userId);
        sysUser.setIsRealUser(SysUserConstant.IS_REAL_USER);
        sysUser.setStatus(Integer.valueOf(SysUserConstant.STATUS));
        sysUser.setDelFlag(Integer.valueOf(SysUserConstant.DEL_FLAG));

        // 企业部门相关信息
        sysUser.setOrgId(SpcyConstant.QY_ORG_ID); // 部门编号
        sysUser.setOrgName(SpcyConstant.QY_ORG_NAME); // 部门名称
        sysUser.setOrgCode(SpcyConstant.QY_ORG_CODE); // 默认机构编码

        // 联系信息
        sysUser.setUsername(username+"_"+realname);
        sysUser.setRealname(realname);
        sysUser.setFgzdw(realname);
        sysUser.setPhone(qy.getFqylxrdh());
        sysUser.setEmail(qy.getFqylxryx());
        sysUser.setFqyid(qy.getId());

        save(sysUser); // 新增用户

        /** 新增部门用户关系 */
        YcSysUserDepart sysUserDepart = new YcSysUserDepart();
        sysUserDepart.setId(GetUuIdUtils.ReplaceUuId());
        sysUserDepart.setUserId(userId);

        // 企业
        sysUserDepart.setDepId(SpcyConstant.QY_ORG_ID); // 企业部门id
        sysUserDepart.setPositionId(""); // 职务编号
        sysUserDepart.setIsMajorDep(SysUserConstant.IS_MAJOR_DEP); // 是否主部门
        ycSysUserMapper.addYcSysUserDept(sysUserDepart);

        /** 对用户角色进行自动授权 */
        YcSysUserRole userRole = new YcSysUserRole();
        userRole.setId(GetUuIdUtils.ReplaceUuId());
        userRole.setUserId(userId);
        userRole.setRoleId(SpcyConstant.QY_ROLE_ID);
        ycSysUserMapper.addYcSysUserRole(userRole);

        // 新增日志
        String addSysUserOper = "新增企业用户：";
        addSysUserOper += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(sysUser));
        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,addSysUserOper,userId, "新增企业用户",SpcyConstant.LOGS_SYS_TYPE_XZ);

        // 用户部门 日志
        /*String ycAddUserDepartOperCont = "新增系统用户部门联系：用户名["+ycSysUser.getRealname()+"]; 部门["+SpcyConstant.HCY_ORGNAME+"]; " +
                "用户编号["+userId+"]; 部门编号["+SpcyConstant.HCY_ORGID+"];";
        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,ycAddUserDepartOperCont,userId,"新增企业用户",SpcyConstant.LOGS_SYS_TYPE_XZ);*/

        // 加新增角色授权的日志
        /*String ycAddUserRoleOperCont = "新增用户角色联系：用户名["+ycSysUser.getRealname()+"]; 用户编号["+userId+"]; " +
                "角色编号["+roleId+"];]";
        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER,ycAddUserRoleOperCont,userId,"新增企业用户",SpcyConstant.LOGS_SYS_TYPE_XZ);*/


    }

    @Override
    public void updateSysYhkOrGwk(SpcyJcyFybxdgrjl oldBean, SpcyJcyFybxdgrjl bxdryBean) {
        if (StringUtils.isBlank(oldBean.getFuserid())){
            return;
        }

        boolean isFlag = false;
        String logNr = "";

        YcSysUser updateBean = new YcSysUser();
        if (StringUtils.isNotBlank(bxdryBean.getFkfh())){
            updateBean.setFkfh(bxdryBean.getFkfh());
            isFlag = true;
            logNr += "开户行：";
            logNr += "从["+ oldBean.getFkfh() +"]设置为"+bxdryBean.getFkfh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFyhkh())){
            updateBean.setFyhkh(bxdryBean.getFyhkh());
            isFlag = true;
            logNr += "银行卡号：";
            logNr += "从["+ oldBean.getFyhkh() +"]设置为"+bxdryBean.getFyhkh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFkhhszd())){
            updateBean.setFkhhszd(bxdryBean.getFkhhszd());
            isFlag = true;
            logNr += "开户行所在地：";
            logNr += "从["+ oldBean.getFkhhszd() +"]设置为"+bxdryBean.getFkhhszd();
        }

        if (StringUtils.isNotBlank(bxdryBean.getFgwkh())){
            updateBean.setFgwkh(bxdryBean.getFgwkh());
            isFlag = true;
            logNr += "公务卡号：";
            logNr += "从["+ oldBean.getFgwkh() +"]设置为"+bxdryBean.getFgwkh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFgwkkhh())){
            updateBean.setFgwkkhh(bxdryBean.getFgwkkhh());
            isFlag = true;
            logNr += "公务卡开户行：";
            logNr += "从["+ oldBean.getFgwkkhh() +"]设置为"+bxdryBean.getFgwkkhh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFgwkszd())){
            updateBean.setFgwkszd(bxdryBean.getFgwkszd());
            isFlag = true;
            logNr += "公务卡所在地：";
            logNr += "从["+ oldBean.getFgwkszd() +"]设置为"+bxdryBean.getFgwkszd();
        }
        if (isFlag) {
            LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(YcSysUser::getId, bxdryBean.getFuserid());

            ycSysUserMapper.update(updateBean, updateWrapper);

            spcyLogService.addLogs(LogConstant.LOGS_JCYGL,logNr,oldBean.getFuserid(),"检查员报销同步修改用户信息",SpcyConstant.LOGS_SYS_TYPE_XG);
        }
    }

    @Override
    public void updateSysYhkOrGwkByHydjry(SpcySqsxHydjry oldBean, SpcySqsxHydjry bxdryBean) {
        if (StringUtils.isBlank(oldBean.getFuserid())){
            return;
        }

        boolean isFlag = false;
        String logNr = "";

        YcSysUser updateBean = new YcSysUser();
        if (StringUtils.isNotBlank(bxdryBean.getFkfh())){
            updateBean.setFkfh(bxdryBean.getFkfh());
            isFlag = true;
            logNr += "开户行：";
            logNr += "从["+ oldBean.getFkfh() +"]设置为"+bxdryBean.getFkfh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFyhkh())){
            updateBean.setFyhkh(bxdryBean.getFyhkh());
            isFlag = true;
            logNr += "银行卡号：";
            logNr += "从["+ oldBean.getFyhkh() +"]设置为"+bxdryBean.getFyhkh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFkhhszd())){
            updateBean.setFkhhszd(bxdryBean.getFkhhszd());
            isFlag = true;
            logNr += "开户行所在地：";
            logNr += "从["+ oldBean.getFkhhszd() +"]设置为"+bxdryBean.getFkhhszd();
        }

        if (StringUtils.isNotBlank(bxdryBean.getFgwkh())){
            updateBean.setFgwkh(bxdryBean.getFgwkh());
            isFlag = true;
            logNr += "公务卡号：";
            logNr += "从["+ oldBean.getFgwkh() +"]设置为"+bxdryBean.getFgwkh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFgwkkhh())){
            updateBean.setFgwkkhh(bxdryBean.getFgwkkhh());
            isFlag = true;
            logNr += "公务卡开户行：";
            logNr += "从["+ oldBean.getFgwkkhh() +"]设置为"+bxdryBean.getFgwkkhh();
        }
        if (StringUtils.isNotBlank(bxdryBean.getFgwkszd())){
            updateBean.setFgwkszd(bxdryBean.getFgwkszd());
            isFlag = true;
            logNr += "公务卡所在地：";
            logNr += "从["+ oldBean.getFgwkszd() +"]设置为"+bxdryBean.getFgwkszd();
        }
        if (isFlag) {
            LambdaUpdateWrapper<YcSysUser> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(YcSysUser::getId, bxdryBean.getFuserid());

            ycSysUserMapper.update(updateBean, updateWrapper);

            spcyLogService.addLogs(LogConstant.LOGS_JCYGL,logNr,oldBean.getFuserid(),"检查员报销同步修改用户信息",SpcyConstant.LOGS_SYS_TYPE_XG);
        }
    }

    @Override
    public String findRealnameById(String id) {
        return ycSysUserMapper.findRealnameById(id);
    }

    @Override
    public String selectFqyidById(String id) {
        return ycSysUserMapper.findFqyidById(id);
    }
}
